//
//  OrderCenterVC.swift
//  Employee
//
//  Created by swordjoy on 2017/3/24.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class OrderCenterVC: BaseVC {
  
  fileprivate var allOrderVC: OrderStateVC!
  fileprivate var acceptingOrderVC: OrderStateVC!
  fileprivate var submitOrderVC: OrderStateVC!
  fileprivate var waitOrderVC: OrderStateVC!
  
  fileprivate var cacheIndexs = Set<Int>()
  
  fileprivate var ant: SJAnt!
  
  override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = GlobalConfig.BGWhiteColor
    title = "工单中心"
    
    create()
  }
  
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    GlobalConfig.baiduStatisticsPageViewStart("工单中心")
  }
  
  override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    GlobalConfig.baiduStatisticsPageViewEnd("工单中心")
  }
  
}

// MARK: - action

extension OrderCenterVC {
  
  @objc fileprivate func addOrderAction() {
    let vc = NewOrderVC()
    navigationController?.pushViewController(vc, animated: true)
  }
  
  @objc fileprivate func searchAction() {
    let vc = OrderSearchVC()
    navigationController?.pushViewController(vc, animated: true)
  }
  
}

// MARK: - fileprivate

extension OrderCenterVC {
  
    fileprivate func create() {
    
        allOrderVC = OrderStateVC()
        allOrderVC.title = "全部工单"
        allOrderVC.parentVC = self
        allOrderVC.type = .all
        
        acceptingOrderVC = OrderStateVC()
        acceptingOrderVC.title = "我受理中"
        acceptingOrderVC.parentVC = self
        acceptingOrderVC.type = .accepting
        
        submitOrderVC = OrderStateVC()
        submitOrderVC.title = "我提交的"
        submitOrderVC.parentVC = self
        submitOrderVC.type = .submit
        
        waitOrderVC = OrderStateVC()
        waitOrderVC.title = "待接工单"
        waitOrderVC.parentVC = self
        waitOrderVC.type = .wait
        
        let VCs = [allOrderVC, acceptingOrderVC, submitOrderVC, waitOrderVC]
        
        let frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: UIScreen.noNavBarHeight)
        ant = SJAnt(frame: frame, VCs: VCs as! [UIViewController], parentVC: self, delegate: self)
        ant.backgroundColor = UIColor.white
        view.addSubview(ant)
        
        addBackItem()
        
        
        let addButton = UIButton()
        addButton.frame = SJApp.bounds(width: 30, height: 44)
        addButton.setImage(UIImage.init(named: "nav-addorder-blue-icon"), for: .normal)
        addButton.addTarget(self, action: #selector(addOrderAction), for: .touchUpInside)
        addButton.contentHorizontalAlignment = .right
        let addItem = UIBarButtonItem(customView: addButton)
        
        let searchButton = UIButton()
        searchButton.frame = SJApp.bounds(width: 30, height: 44)
        searchButton.addTarget(self, action: #selector(searchAction), for: .touchUpInside)
        searchButton.setImage(UIImage.init(named: "search-icon-blue"), for: .normal)
        searchButton.contentHorizontalAlignment = .right
        let searchItem = UIBarButtonItem(customView: searchButton)
        
        let spaceItem = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
        spaceItem.width = -5
        
        self.navigationItem.rightBarButtonItems = [spaceItem, addItem, searchItem]
    }
}

extension OrderCenterVC: SJAntDelegate {
  func SJAntIndexChange(index: Int) {
    update(with: index)
  }
  
  fileprivate func update(with index: Int) {
    
    switch index {
    case 0:
      self.allOrderVC.updateData()
    case 1:
      self.acceptingOrderVC.updateData()
    case 2:
      self.submitOrderVC.updateData()
    case 3:
      self.waitOrderVC.updateData()
      
    default: break
      
    }
  }
  
}
